<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="../static/js/jquery-1.11.0.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		$("#btn").click(function() {
			var vname = $('#name').val();
			var vage = $('#age').val();
			var vgender = $("input[name='gender']:checked").val();
			var checkedParams = $("input:checkbox:checked");
			checked_val = [];
			for(k in checkedParams){
		        if(checkedParams[k].checked)
		            checked_val.push(checkedParams[k].value);
		        
		    };
		    var arr = new Array();
			for(var i = 0; i < checked_val.length; i++) {
				arr.push({'addressName':checked_val[i]});
			}
			var arrs = JSON.stringify(arr);
			var params = {
				name : vname,
				age : vage,
				gender : vgender,
				addresses:arrs
			}
			
			var params2 = '{"name":\"'+vname+'\","age":'+vage+',"gender":\"'+vgender+'\","addresses":'+arrs+'}'
			console.log("params=" + JSON.stringify(params));
			console.log("params2=" + params2);
			$.ajax({
				url : "add",
				type : 'POST',
				data : params2, //①
				//data:JSON.stringify(params), //②
				dataType : 'json',
				contentType : "application/json;charset=utf-8",
				beforeSend: function() {
				},
				success : function(data) {
					alert("保存成功");
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					console.log(textStatus + "服务器异常，请稍后尝试！");
				}
			});
		});
	});
</script>
</head>
<body>
	姓名：<input type="text" id="name" /><br/>
	年龄：<input type="text" id="age" /><br/>
	性别:
	男:<input type="radio" checked="checked" name="gender" value="男"/>
	女：<input type="radio" name="gender" value="女"/><br/>
	地址:
	A区<input type="checkbox" id="checkbox" value="A区" />
	B区<input type="checkbox" id="checkbox" value="B区" />
	C区<input type="checkbox" id="checkbox" value="B区" /><br/>
	<button id="btn" >保存</button>
</body>
</html>